﻿using AnyCAD.Foundation;
using AnyCAD.NX.Plugin;
using Rapid.CAX.ViewModel;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Windows;
using System.Windows.Controls;

namespace RapidUI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow
    {
        private static MainWindow? mInstance = null;
        public static MainWindow? Instance() { return mInstance; }
        ProjectBrowserViewModel _ProjectBrowserViewModel;
        public MainWindow()
        {
            mInstance = this;
            InitializeComponent();
            var viewModel = new RapidMainViewModel(mView3d, this.mOutputBox); 
            _ProjectBrowserViewModel = new ProjectBrowserViewModel(viewModel, this.mProjectBrower);
            this.DataContext = viewModel;
        }

        public Fluent.Ribbon RibboBar { get => mRibbon; }

        public ICSharpCode.AvalonEdit.TextEditor TextEditor { get => textEditor; }

        public IRenderView Viewer3D { get => mView3d; }

        public RapidMainViewModel MainViewModel { get => (RapidMainViewModel)this.DataContext; }

        public TreeView ProjectBrowserCtrl { get => this.mProjectBrower; }

        private void RibbonWindow_Loaded(object sender, RoutedEventArgs e)
        {
            ////初始化
            //PluginManager.Instance.LoadPlugin(Assembly.GetExecutingAssembly(), 0);
            ////加载外部路径指定的插件
            //PluginManager.Instance.LoadPlugins(System.AppDomain.CurrentDomain.BaseDirectory, 0);

            PluginConfig.Load(MainViewModel);
        }

        private void MView3d_ViewerReady()
        {
            //初始化视图
            MainViewModel.Initialize(mView3d);
            //在这里做自定义的设置
            //TODO:
        }

        private void RibbonWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
          
            PluginManager.Instance.UnloadPlugins();
        }

        private void RibbonWindow_StateChanged(object sender, EventArgs e)
        {
            if (this.WindowState == WindowState.Maximized || this.WindowState == WindowState.Normal)
                mView3d.ForceUpdate();
        }
    }
}
